package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.EarningRecordDAO;
import com.zerosky.crm.po.EarningRecord;
import com.zerosky.crm.service.EarningRecordService;
import com.zerosky.crm.util.PageInfo;

/**
 * Earningrecord业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="earningRecordService")
public class EarningRecordServiceImpl implements EarningRecordService {

	@Autowired
	private EarningRecordDAO earningrecordDAO; // Earningrecord数据接口

	/**
	 * Earningrecord列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public List<EarningRecord> getEarningRecordList(Map map) {
		return earningrecordDAO.getEarningRecordList(map);
	}

	/**
	 * 通过主键查找Earningrecord实体
	 * 
	 * @param id
	 *            Earningrecord主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public EarningRecord getEarningRecordById(String id) {
		return earningrecordDAO.getEarningRecordById(id);
	}

	/**
	 * 保存Earningrecord
	 * 
	 * @param Earningrecord
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveEarningRecord(EarningRecord earningrecord, String operation) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
			earningrecordDAO.addEarningRecord(earningrecord);
		} else if ("修改".equalsIgnoreCase(operation)) {
			earningrecordDAO.updateEarningRecord(earningrecord);
		}

	}

	/**
	 * 批量删除Earningrecord
	 * 
	 * @param idArray
	 *            Earningrecord主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteEarningRecordById(String id) throws Exception {
		earningrecordDAO.deleteEarningRecordById(id);

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<EarningRecord> getPageInfoList(Map map, PageInfo<EarningRecord> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<EarningRecord> list = earningrecordDAO.getEarningRecordList(map);
		page.setList(list);

		int count = earningrecordDAO.countEarningRecord(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}
}
